package com.googlecode.activitiportal.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HashUtil {
	
	public static final String DIGEST_MD5 = "MD5";

	public static byte[] getHash(String s, String algorithm) {
		try {
			MessageDigest md = MessageDigest.getInstance(algorithm);
			md.update(s.getBytes());
			return md.digest();
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
	}

	public static String getHexa(byte[] bytes) {
		StringBuilder s = new StringBuilder();
		for (int i = 0; i < bytes.length; i++) {
			int high = ((bytes[i] >> 4) & 0xf) << 4;
			int low = bytes[i] & 0xf;
			if (high == 0)
				s.append('0');
			s.append(Integer.toHexString(high | low));
		}
		return s.toString();
	}

	public static String getHashString(String s, String algorithm) {
		return getHexa(getHash(s, algorithm));
	}
}